package com.ex.mzwqz.widget;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.ex.mzwqz.R;
import com.ex.mzwqz.adapter.AdPageAdapter;
import com.ex.mzwqz.widget.viewpagerindicator.CirclePageIndicator;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 自动滑动的banner
 * Created by akira on 2016/4/27.
 */
public class AutoScrollBannerView extends RelativeLayout implements ViewPager.OnPageChangeListener, View.OnTouchListener {

    private Context context;
    private ViewPager vpAdPic;
    private CirclePageIndicator llIndicator;

    //    ViewPager vpAdPic;

    private int currentPosition;
    private static final int MSG = 0X100;
    /**
     * 轮播图最大数
     *//*
    private int totalCount = Integer.MAX_VALUE;
*/
    /**
     * 用户是否干预
     */
    private boolean isUserTouched = false;
    /**
     * 默认的轮播时间
     */
    private static final int DEFAULT_TIME = 3000;
    /**
     * 设置轮播时间
     */
    private int switchTime = DEFAULT_TIME;
    /**
     * 轮播图定时器
     */
    private Timer mTimer ;

    private TimerTask mTimerTask ;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == MSG) {
                Log.e("Pos", "the position is " + currentPosition);
                if (currentPosition < vpAdPic.getAdapter().getCount() - 1) {
                    currentPosition = currentPosition + 1;
                    vpAdPic.setCurrentItem(currentPosition, true);
                } else {
                    currentPosition = 0;
                    vpAdPic.setCurrentItem(currentPosition, false);
                }
            }
        }
    };

    public AutoScrollBannerView(Context context) {
        this(context, null);
        this.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT));
    }

    public AutoScrollBannerView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }


    public AutoScrollBannerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
//        initBanner();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
//        inflater = LayoutInflater.from(context);
//        View view = inflater.inflate(R.layout.view_page_ads, null);
        vpAdPic = (ViewPager) findViewById(R.id.vp_adpic);
        vpAdPic.setOffscreenPageLimit(5);//设置5个缓存
        llIndicator = (CirclePageIndicator) findViewById(R.id.ll_indicator);
        vpAdPic.setOnPageChangeListener(this);
        vpAdPic.setOnTouchListener(this);
        //以指定周期和岩石开启一个定时任务
        startTimer();
//        addView(view);
    }

    private void startTimer() {
        if (mTimer==null)
        mTimer = new Timer();
        if (mTimerTask==null)
        mTimerTask = new TimerTask() {
            @Override
            public void run() {
                //用户滑动时，定时任务不响应
                if (!isUserTouched) {
//                currentPosition = (currentPosition + 1) % totalCount;
                    handler.sendEmptyMessage(MSG);
                }
            }
        };
        mTimer.schedule(mTimerTask, switchTime, switchTime);
    }

    private void initBanner() {


    }


    //设置adapter，这个方法需要再使用时设置
    public void setAdapter(AdPageAdapter adapter) {
        if (adapter != null) {
            vpAdPic.setAdapter(adapter);
            llIndicator.setViewPager(vpAdPic);
        }
    }
//
//    //设置adapter，这个方法需要再使用时设置
//    public void setImageAdAdapter(ImageAdPageAdapter adapter, boolean isVideo) {
//        if (adapter != null) {
//            adapter.setVideo(isVideo);
//            vpAdPic.setAdapter(adapter);
//            llIndicator.setViewPager(vpAdPic);
//        }
//    }
//
//    //设置adapter，这个方法需要再使用时设置
//    public void setVideoAdAdapter(VideoAdPageAdapter adapter, boolean isVideo) {
//        if (adapter != null) {
//            adapter.setVideo(isVideo);
//            vpAdPic.setAdapter(adapter);
//            llIndicator.setViewPager(vpAdPic);
//        }
//    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        currentPosition = position;
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        switch (event.getAction()) {

            case MotionEvent.ACTION_UP:
                isUserTouched = false;
                break;

            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                isUserTouched = true;
                break;
        }

        return false;
    }

    public void cancenTask(){

        mTimerTask.cancel();
        mTimerTask = null;
        mTimer.cancel();
        mTimer = null;
    }

    public void restartTask(){
        startTimer();
    }

    public CirclePageIndicator getLlIndicator(){
        return llIndicator;
    }
}
